package io.renren.modules.live.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.live.entity.FanweAnchorEntity;
import io.renren.modules.live.service.FanweAnchorService;
import io.renren.modules.live.vo.AnchorVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 主播表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-17 09:18:22
 */
@RestController
@Api(tags = "后台主播管理")
@RequestMapping("live/fanweanchor")
@Log4j2
public class FanweAnchorController {
    @Autowired
    private FanweAnchorService fanweAnchorService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("live:fanweanchor:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fanweAnchorService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{sex}")
    @RequiresPermissions("live:fanweanchor:info")
    public R info(@PathVariable("sex") Integer sex){
		FanweAnchorEntity fanweAnchor = fanweAnchorService.getById(sex);

        return R.ok().put("fanweAnchor", fanweAnchor);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("live:fanweanchor:save")
    public R save(@RequestBody FanweAnchorEntity fanweAnchor){
		fanweAnchorService.save(fanweAnchor);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("live:fanweanchor:update")
    public R update(@RequestBody FanweAnchorEntity fanweAnchor){
		fanweAnchorService.updateById(fanweAnchor);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("live:fanweanchor:delete")
    public R delete(@RequestBody Integer[] sexs){
		fanweAnchorService.removeByIds(Arrays.asList(sexs));

        return R.ok();
    }

    /**
     * 佟卢海 - 2020.11.23
     *
     * 管理员查看主播列表
     */
    @PostMapping("/selectAnchorList")
    @ApiOperation("管理员查看主播列表")
    @RequiresPermissions("liveAdministration:ancho")
    public R selectAnchorList(@RequestBody AnchorVo anchorVo){
        R result=new R();
        try{
            if(anchorVo.getPageIndex()==null || anchorVo.getPageSize() ==null){
                result.put("code",9999);
                result.put("msg","页码页容不能为空！！");
                result.put("data",null);
                return result;
            }
            Map<String, Object> stringObjectMap = fanweAnchorService.selectAnchorList(anchorVo);
            result.put("data",stringObjectMap);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }

    /**
     * 佟卢海 - 2020.11.23
     *
     * 新建主播
     */
    @PostMapping("/insertAnchor")
    @ApiOperation("新建主播")
    @RequiresPermissions("liveAdministration:ancho")
    public R insertAnchor(@RequestBody FanweAnchorEntity fanweAnchorEntity){
        R result=new R();
        try{
            if(fanweAnchorEntity.getNickNumber()==null || fanweAnchorEntity.getUserPwd()==null ){
                result.put("code",9999);
                result.put("msg","参数错误！！");
                result.put("data",null);
                return result;
            }
            FanweAnchorEntity fanweAnchorEntity1 = fanweAnchorService.insertAnchor(fanweAnchorEntity);
            result.put("data",fanweAnchorEntity1);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","新建失败！！");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 - 2020.11.23
     *
     * 获取主播账号
     */
    @GetMapping("/obtainNickNumber")
    @ApiOperation("获取主播账号")
    @RequiresPermissions("liveAdministration:ancho")
    public R obtainNickNumber(){
        R result=new R();
        try{
            String s = fanweAnchorService.obtainNickNumber();
            result.put("data",s);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","获取失败！！！");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 - 2020.11.23
     *
     * 管理员查看主播信息
     */
    @GetMapping("/selectAnchorInformation")
    @ApiOperation("管理员查看主播列表")
    @RequiresPermissions("liveAdministration:ancho")
    public R selectAnchorInformation(String anchorId){
        R result=new R();
        try{
            if(StringUtils.isEmpty(anchorId)){
                result.put("code",9999);
                result.put("msg","anchorId能为空！！");
                result.put("data",null);
                return result;
            }
            FanweAnchorEntity fanweAnchorEntity = fanweAnchorService.selectAnchorInformation(anchorId);
            result.put("fanweAnchorEntity",fanweAnchorEntity);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","查询失败");
            result.put("data",null);
        }
        return result;
    }
    /**
     * 佟卢海 - 2020.11.23
     *
     * 修改主播信息状态
     */
    @GetMapping("/updateAnchorInStatus")
    @ApiOperation("获取主播账号")
    @RequiresPermissions("liveAdministration:ancho")
    public R updateAnchorInStatus(String anchorId,String status){
        R result=new R();
        try{
            Integer integer = fanweAnchorService.updateAnchorInStatus(anchorId, status);
        }catch (Exception e){
            log.info(e);
            result.put("code",9999);
            result.put("msg","获取失败！！！");
            result.put("data",null);
        }
        return result;
    }

}
